package com.angel.web.app;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.FileUpload;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzCpr;
import com.angel.health.BuzCustomer;
import com.angel.provider.health.BuzCprProvider;
/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年9月22日 下午7:13:33 类说明 app电子病例
 */
@Controller
@RequestMapping("app/cpr")
public class AppCprController {

	@Autowired
	private BuzCprProvider provider;
	
	@RequestMapping("/index")
	public String index(BuzCpr cpr,HttpServletRequest request,Pager pager){
		cpr.setEnable(1);
		Pager bean = provider.findAll(cpr,pager);
		request.setAttribute("pager", bean);
		return "cpr";
	}
	
	/**
	 * 
	 * @param cpr
	 * @param multiFileMap 所有上传文件
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addCpr",produces="application/json; charset=UTF-8")
	public String save(BuzCpr cpr,MultipartHttpServletRequest request){
		try {
			MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
			String uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
			System.err.println("上传图片------"+uploadUrl);
			cpr.setUploadUrl(uploadUrl);
			int i = provider.insert(cpr);
			
			BuzCpr e = new BuzCpr();
			e.setCode(CodeUtil.createCode(null, Constants.CPR, cpr.getId()));
			e.setId(cpr.getId());
			provider.updateById(e);
			if(i>0){
				return JsonUtil.toJson(new JsonEntity("200", cpr));
			}else{
				return JsonUtil.toJson(new JsonEntity("-1", "失败"));
			}
		} catch (Exception e) {
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}
	
	/**
	 * 删除电子病例
	 * @param buzExamination
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "del", produces = "application/json; charset=UTF-8")
	public String del(BuzCpr cpr) {
		int delById = provider.delById(cpr);
		if (delById > 0) {
			return JsonUtil.toJson(new JsonEntity("200", cpr));
		} else {
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}

	/**
	 * 编辑电子病例
	 * 
	 * @param buzExamination
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "edit", produces = "application/json; charset=UTF-8")
	public String editExamination(BuzCpr cpr,MultipartHttpServletRequest request) {
		if (cpr.getId() == 0 || cpr.getId() == null) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
		String uploadUrl;
		try {
			uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
			cpr.setUploadUrl(uploadUrl);
			int updateById = provider.updateById(cpr);
			if (updateById > 0) {
				return JsonUtil.toJson(new JsonEntity("200", cpr));
			} else {
				return JsonUtil.toJson(new JsonEntity("-1", "失败"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}
}
